package net.hctek.identity.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

import net.hctek.identity.bean.User;
import net.hctek.identity.service.IdentityServiceI;
import net.hctek.identity.util.MD5;
import net.hctek.identity.util.ResponseJsonUtils;

public class RegisteredAction extends ActionSupport{
private static final long serialVersionUID = 1L;
	
	@Autowired
	private IdentityServiceI identityService;
	
	private User user;
	
	private String code;
	private String tip;
	
	@Override
	public String execute(){
		 HttpServletResponse response = ServletActionContext.getResponse();
		 Map<String,String> data=new HashMap<String,String>();
		try {
			 String vcode = (String)ServletActionContext.getRequest().getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
	         System.out.println("vcode:"+vcode+"code:"+code);
	         if(!vcode.equals(code)){
	        	 data.put("message", "验证码错误");
	         }else{
	        	if(identityService.ajaxValidName(user.getUserId())){
				User u = identityService.getUserById(user.getUserId());
				data.put("message", "用户名重复!");
				if(u != null && u.getDelFlag().equals("0")){
					identityService.updateUser(u);
					data.put("message", "该用户以前被删除过，现已复原。");
					}
	        	}else{
	    			String password = MD5.EncoderByMd5(user.getPassWord());
	    			user.setPassWord(password);
					identityService.savaUser(user);
					//清空账号
					user.setUserId("");
					data.put("message", "创建成功");
				}
	         }	
		} catch (Exception e) {
			e.printStackTrace();
			data.put("message", "创建失败!");
			tip = "创建失败！";
		}
		 ResponseJsonUtils.json(response, data);
		return NONE;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}
	
	
}
